package es.ignapziriza.ccrypto.ciphers.transposition;

import es.ignapziriza.ccrypto.Cipher;
import es.ignapziriza.ccrypto.CipheredText;

/**
 * Scytale Ciphering. <br>
 * (for more information, see <a
 * href="http://en.wikipedia.org/wiki/Scytale">Wikipedia</a>)
 * 
 * @see CipheredText
 * @see Cipher
 * @author ignapziriza
 * 
 */
public class ScytaleCipher extends CipheredText implements Cipher {

	protected int diameter;

	/**
	 * Default constructor.
	 * 
	 * @author ignapziriza
	 */
	public ScytaleCipher() {
		diameter = 4;
	}

	/**
	 * Constructor.
	 * 
	 * @param scytale_diameter
	 *            the imaginary diamieter (shift) in the Scytale.
	 * @author ignapziriza
	 */
	public ScytaleCipher(int scytale_diameter) {
		diameter = scytale_diameter;
	}

	@Override
	public void cipher(String textToCipher) {
		cipheredText = "";
		int phase = textToCipher.length() / diameter;
		for (int i = 0; i < phase; i++) {
			for (int j = i; j < textToCipher.length(); j += phase) {
				cipheredText += textToCipher.charAt(j);
			}
		}
	}

	@Override
	public void decipher(String textToDecipher) {
		cipheredText = "";
		for (int i = 0; i < diameter; i++) {
			for (int j = i; j < textToDecipher.length(); j += diameter) {
				cipheredText += textToDecipher.charAt(j);
			}
		}

	}

}
